/*
 * @Author: wjk
 * @Date: 2020-01-03 17:02:41
 * @LastEditors: wjk
 * @LastEditTime: 2020-07-13 09:39:35
 * @description: file content
 */
const pType = {
  'h5': 'BIXUAN_H5',
  'mp-weixin': 'BIXUAN_MINI'
}
export const platformType = pType[process.env.VUE_APP_PLATFORM]
const userInfo = uni.getStorageSync('biXuanWorld') || null
const genderText = ['保密', '男', '女']
let sensor
let register

// #ifdef MP-WEIXIN
import saMini from 'sa-sdk-miniprogram'
saMini.setPara({
  server_url: process.env.VUE_APP_SENSOR_URL,
  show_log: false // process.env.NODE_ENV === 'development'
})
saMini.init()
sensor = saMini
register = saMini.registerApp
// #endif

// #ifdef H5
var saJs = require('sa-sdk-javascript')
saJs.init({
  server_url: process.env.VUE_APP_SENSOR_URL,
  is_track_single_page: true,
  show_log: false, // process.env.NODE_ENV === 'development',
  heatmap: {
    clickmap: 'default',
    scroll_notice_map: 'not_collect'
  }
})
sensor = saJs
register = saJs.registerPage
// #endif

export function sensorRegister(properties) {
  sensor.login(properties.uid)
  sensor.setProfile({
    platformType,
    ...properties,
    gender: properties ? genderText[properties.gender] : ''
  })
  register({
    platformType,
    ...properties,
    gender: properties ? genderText[properties.gender] : ''
  })
}
export function sensorTrack(event, properties) {
  // console.log(event, properties)
  sensor.track(event, properties)
}

// 初始化时注册一次
sensorRegister({
  platformType,
  uid: userInfo ? userInfo.uid : sensor.quick('getAnonymousID'),
  gender: userInfo ? genderText[userInfo.gender] : '',
  mobile: userInfo ? userInfo.mobile : ''
})
export default sensor
